package com.java.mogodb;
import com.mongodb.MongoClient;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.model.Filters;
import com.mongodb.client.model.Updates;
import com.mongodb.client.result.InsertOneResult;
import org.bson.Document;
import java.util.Scanner;

public class LoginDetailsOfMongoDb {
    public static void main(String[] args) {
        LoginDetailsOfMongoDb.dataConnect();
    }
    public static void dataConnect(){
        MongoClient mongoClient = new MongoClient("mongodb://localhost:27017");
        MongoDatabase database= mongoClient.getDatabase("Login");
        String dataConnect= "Login_details";
        Scanner sc =new Scanner(System.in);
        System.out.println("Select Options\n1. Create\n2. Delete\n3. Update\n4. Find details\n5. Display All");
        int choice = Integer.parseInt(sc.next());
        switch (choice){
            case 1->{
                //INSERTING SINGLE DOCUMENT
                System.out.print("Enter your name: ");
                String name=sc.next();
                System.out.print("Enter your city name: ");
                String city= sc.next();
                System.out.print("Enter your e-mail: ");
                String email = sc.next();
                InsertOneResult inserted = database.getCollection(dataConnect).insertOne(new Document("Name", name).append("City", city).append("Email", email));
                System.out.println("Inserted: "+inserted);
            }case 2->{
                //DELETING SINGLE DOCUMENT
                System.out.print("Enter name to delete: ");
                String name= sc.next();
                Document deleteDoc = database.getCollection(dataConnect).findOneAndDelete(Filters.eq("Name",name));
                if (deleteDoc==null){
                    System.out.println("Please enter correct Name!!!");
                }else {
                    System.out.println("Deleted Document:"+deleteDoc);
                }
            }
            case 3-> {
                //UPDATING THE DETAILS OF DOCUMENT
                System.out.print("Enter name to update");
                String name = sc.next();
                System.out.println("select to update in this\n1. name\n2. city\n3. email");
                int selectToUpdate = Integer.parseInt(sc.next());
Document updateDoc=null;
                switch (selectToUpdate) {
                    case 1 -> {
                        String newName = sc.next();
                        Document updateName = database.getCollection(dataConnect)
                                .findOneAndUpdate(Filters.eq("Name", name), Updates.set("Name", newName));
                    }
                    case 2 -> {
                        String city = sc.next();
                        Document updateCity = database.getCollection(dataConnect).findOneAndUpdate(Filters.eq("Name", name), Updates.set("City", city));
                    }
                    case 3 -> {
                        String email = sc.next();
                        Document updateMail = database.getCollection(dataConnect).findOneAndUpdate(Filters.eq("Name", name), Updates.set("Email", email));
                    }
                }
                if (name!=null){
                    System.out.println("No document found with that name."+name);
                }else {
                    System.out.println("Updated Document: "+updateDoc);
                }
            }
            case 4-> {
                //FINDING SPECIFIC DOCUMENT
                System.out.print("Enter name to view details: ");
                String name = sc.next();
               if (name!=null) System.out.println("No document found for name: "+name);else
                   database.getCollection(dataConnect)
                       .find(Filters.eq("Name",name)).forEach(doc-> System.out.println(doc.toJson()));
            }
            case 5->{
                //DISPLAYING ALL DOCUMENTS
               if (database==null)
                   System.err.println("This Collection has no Data");
               else System.out.println(" view All Details ");  database.getCollection(dataConnect).find().forEach(document -> System.out.println(document.toJson()));
            }
        }
        mongoClient.close();
    }
}

